import {
  Button,
  Form,
  FormItem,
  Input,
  InputNumber,
  message,
  Modal,
  Radio,
  RadioGroup,
  Space,
  TypographyText
} from 'ant-design-vue'
import { fa } from 'element-plus/lib/locale'
import { defineComponent, nextTick, onMounted, ref } from 'vue'
import { omit } from 'xe-utils'

import { jindouGiftApi, rechargeApi } from '@/apis'
import aiTestSkinApi from '@/apis/aiTestSkinApi'
import { ExpressTempleteItem } from '@/apis/jindouGiftTypes'
import { type ProTableProps, type ProTableRef, ProTable } from '@/components'
import UploadFile from '@/components/UploadFile'
import { TABLE_FORMAT, TABLE_ITEM_RENDER, TABLE_RENDERER } from '@/constants'
import { useDialog } from '@/hooks'
import router from '@/router'

export default defineComponent({
  name: 'JdgExpressTemplete',
  setup() {
    const showDialog = ref(false)
    const dialogLoading = ref(false)
    const optionItemCur = ref({} as ExpressTempleteItem)
    const { showConfirm } = useDialog()

    const tableRef = ref({} as ProTableRef)
    const tableConfig = ref({
      fixedHeight: true,
      options: {
        border: true,
        align: 'center',
        toolbarConfig: {
          slots: {
            buttons: 'batchButtons'
          }
        },
        formConfig: {
          items: [
            {
              title: '小程序名称',
              field: '',
              itemRender: {
                name: TABLE_ITEM_RENDER.AInput,
                props: {
                  placeholder: '请输入',
                  allowClear: true
                }
              }
            },
            {
              itemRender: {
                name: TABLE_RENDERER.FormItemBtns
              }
            }
          ]
        },
        columnConfig: {
          minWidth: 100,
          resizable: true
        },
        columns: [
          {
            title: '小程序名称',
            field: 'analysisDimension'
          },
          {
            title: 'APPID',
            field: 'degreeAnalysis',
            minWidth: 150
          },
          {
            title: '限制类型',
            field: 'suggestion'
          },
          {
            title: '次数',
            field: 'popularScienceCopy'
          },
          {
            title: '状态',
            fixed: 'state',
            slots: {
              default: 'state'
            },
            width: 100
          },
          {
            title: '操作',
            fixed: 'right',
            slots: {
              default: 'operation'
            },
            width: 180
          }
        ],
        pagerConfig: {
          pageSize: 15
        },
        proxyConfig: {
          seq: true,
          form: true,
          props: {
            result: 'dataList',
            total: 'page.totalCount'
          },
          ajax: {
            query: async ({ page, form }) => {
              if (!form.analysisDimension) {
                form.analysisDimension = '毛孔'
              }
              const data = await aiTestSkinApi.getCopyWriting({
                pageParameter: {
                  pageSize: page.pageSize,
                  currentPage: page.currentPage
                },
                searchConText: form.analysisDimension,
                ...form
              })
              return data
            }
          }
        }
      }
    } as ProTableProps)

    //启用禁用操作
    function handleForbidden(item) {
      showConfirm({
        content:
          item.state == 1 ? '确认禁用检测症状文案？' : '确认启用检测症状文案？',
        onOk: async () => {
          try {
            await aiTestSkinApi.getDisableEnable({
              id: item.id,
              state: item.state == 0 ? 1 : 0
            })
            tableRef.value.refresh()
          } catch (error) {}
        }
      })
    }

    //新增
    function add() {
      showDialog.value = true
    }

    // 新增确定
    async function dialogConfirm() {
      // if (!optionItemCur.value.bannerUrl) {
      //   message.error('请上传图片')
      //   return
      // }
      // if (!optionItemCur.value.state) {
      //   message.error('请选择状态')
      //   return
      // }

      try {
        dialogLoading.value = true

        await aiTestSkinApi.getBottomPicture({
          // bannerUrl: optionItemCur.value.bannerUrl,
          // state: optionItemCur.value.state
        })
        showDialog.value = false
        tableRef.value.refresh()
      } finally {
        dialogLoading.value = false
      }
    }
    //编辑
    function handEdit(row) {
      router.push({ name: 'AiCopyEdit', query: { id: row.id } })
    }

    return {
      tableRef,
      tableConfig,
      showDialog,
      dialogLoading,
      optionItemCur,
      dialogConfirm,
      add,
      handleForbidden,
      handEdit
    }
  },
  render() {
    return (
      <div>
        <ProTable
          ref="tableRef"
          v-slots={{
            batchButtons: () => (
              <Space>
                <Button type="primary" onClick={() => this.add()}>
                  新增
                </Button>
              </Space>
            ),
            state: ({ row }) => {
              return (
                <Space>
                  {row.state == 0 ? (
                    <TypographyText type="danger">禁用</TypographyText>
                  ) : (
                    <TypographyText type="success">启用</TypographyText>
                  )}
                </Space>
              )
            },

            operation: ({ row }) => (
              <Space>
                <Button
                  type="link"
                  size="small"
                  onClick={() => this.handEdit(row)}
                >
                  编辑
                </Button>
                <Button
                  type="link"
                  size="small"
                  onClick={() => this.handleForbidden(row)}
                >
                  {row.state === 0 ? '启用' : '禁用'}
                </Button>
              </Space>
            )
          }}
          {...this.tableConfig}
        />
        <Modal
          v-model:visible={this.showDialog}
          confirmLoading={this.dialogLoading}
          title={'新增'}
          destroyOnClose
          onOk={this.dialogConfirm}
        >
          <Form labelCol={{ span: 6 }}>
            <FormItem label="小程序名称" required>
              <Input></Input>
            </FormItem>
            <FormItem label="APPID" required>
              <Input></Input>
            </FormItem>
            <FormItem label="限制类型" required>
              <Input></Input>
            </FormItem>
            <FormItem label="次数" required>
              <Input></Input>
            </FormItem>
            <FormItem label="状态：" required>
              <RadioGroup v-model:value={this.optionItemCur.state}>
                <Radio value={1}>启用</Radio>
                <Radio value={0}>禁用</Radio>
              </RadioGroup>
            </FormItem>
          </Form>
        </Modal>
      </div>
    )
  }
})
